//
//  ShellCoinView.m
//  ShellWallert
//
//  Created by lvzhao on 2020/5/10.
//  Copyright © 2020 VV. All rights reserved.
//

#import "ShellCoinView.h"
#import "ShellCoinHeadView.h"
#import "ShellCoinViewModel.h"
#import "ShellCoinCell.h"
#import "LZBaseTableView.h"
#import "ShellCoinDetailViewController.h"
#import "OYCountDownManager.h"


@interface ShellCoinView()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong) ShellCoinViewModel *viewModel;

@property (nonatomic,strong) LZBaseTableView *tableView;

@property (nonatomic,strong) ShellCoinHeadView *headView;


@end

@implementation ShellCoinView

- (instancetype)initViewModel:(LZBaseViewModel *)viewModel
{
    self = [super init];
    if (self) {
        
        self.viewModel = (ShellCoinViewModel *)viewModel;
        [self setupView];
    }
    return self;
}


#pragma mark - setupView
- (void)setupView{

    ShellCoinHeadView *headView = [[ShellCoinHeadView alloc] initViewModel:self.viewModel];
    headView.frame = CGRectMake(0, 0, K_SCREENWIDTH, (K_SCREENWIDTH - 40) * 140 /334 + 160 + 10);
    
    [self addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(0);
    }];
    self.tableView.tableHeaderView = headView;


    @weakify(self);
    [self.viewModel.reloadSubject subscribeNext:^(id  _Nullable x) {
        @strongify(self);

        [self reloadRecordData:[x integerValue]];
    }];


    [self.tableView addHeadWithRefreshingBlock:^{
        @strongify(self);
        [self.viewModel getCoinList:YES];
    }];
    
    [self.tableView addAutoNormalFooterWithRefreshingBlock:^{
        
        @strongify(self);
        [self.viewModel getCoinList:NO];
    }];
    
    self.tableView.mj_footer.hidden = YES;
    
    [[[NSNotificationCenter defaultCenter] rac_addObserverForName:k_APPLanguageWillChangeNotificationName object:nil] subscribeNext:^(NSNotification * _Nullable x) {
        @strongify(self);
        [self.tableView reloadData];
    }];
}

- (void)reloadRecordData:(NSInteger)type{
    dismiss(nil);
    if(type > 4)return;
   //定时器刷新
    [kCountDownManager reload];
    
    [self.tableView reloadData];
    self.tableView.mj_header.state  = MJRefreshStateIdle;
    self.tableView.mj_footer.hidden = NO;
    self.tableView.mj_footer.state  = MJRefreshStateIdle;
    switch (type) {
       case 0:{
           self.tableView.mj_footer.state = MJRefreshStateNoMoreData;
       }
           break;
       default:
           break;
    }
    
    
}

#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.viewModel.dataArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    ShellCoinCell *coinCell = [tableView dequeueReusableCellWithIdentifier:@"ShellCoinCell"];
    if(self.viewModel.dataArray.count > indexPath.row){
        coinCell.modelObject = self.viewModel.dataArray[indexPath.row];
    }
    return coinCell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    ShellCoinDetailViewController *detailVC = [[ShellCoinDetailViewController alloc] init];
    if(self.viewModel.dataArray.count > indexPath.row){
        ShellCoinModel *coinModel = self.viewModel.dataArray[indexPath.row];
       detailVC.title = coinModel.periodStr;
       detailVC.coinModel = self.viewModel.dataArray[indexPath.row];
       [LZTool.currentViewController.navigationController pushViewController:detailVC animated:YES];
    }
   
}

#pragma mark - get && set
- (LZBaseTableView *)tableView{
    if(!_tableView){
        _tableView = [[LZBaseTableView alloc] init];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor = COLOR_TINT;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.showsVerticalScrollIndicator = NO;
        [_tableView registerClass:[ShellCoinCell class] forCellReuseIdentifier:@"ShellCoinCell"];
        _tableView.rowHeight = 145;
    }
    return _tableView;
}


@end
